package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Grade;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.enums.SourseLeveEnum;
import com.xmy.cultivate.service.IChangeSchoolContentService;
import com.xmy.cultivate.service.IGradeService;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.ExcelExportHandle;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 转校内容 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-04-06
 */
@RestController
@RequestMapping("/api/changeSchoolContent")
public class ChangeSchoolContentController extends BaseController {

    @Autowired
            @Lazy
    IChangeSchoolContentService iChangeSchoolContentService;

    @Autowired
    @Lazy
    IGradeService iGradeService;

    @Autowired
    @Lazy
    IQuarterService iQuarterService;

    @GetMapping("/gradeExcel")
    @ApiOperation("转校列表导出")
    public Result gradeExcel(@RequestParam Map<String, String> reMap) {//1老师，2校区老师，3校长，4校区
        String schoolId = reMap.get("schoolIdList") != null && !reMap.get("schoolIdList").isEmpty() ? reMap.get("schoolIdList") : "" ;
        String classRoomId = reMap.get("classRoomIdList") != null && !reMap.get("classRoomIdList").isEmpty() ? reMap.get("classRoomIdList") : "" ;
        String courseId = reMap.get("courseIdList") != null && !reMap.get("courseIdList").isEmpty() ? reMap.get("courseIdList") : "" ;
        String yearPart = reMap.get("yearPart") != null && !reMap.get("yearPart").isEmpty() ? reMap.get("yearPart") : "" ;
        String quarterNum = reMap.get("quarterNumList") != null && !reMap.get("quarterNumList").isEmpty() ? reMap.get("quarterNumList") : "" ;
        String lessonType = reMap.get("lessonType") != null && !reMap.get("lessonType").isEmpty() ? reMap.get("lessonType") : "1" ;
        String teacherName = reMap.get("teacherName") != null && !reMap.get("teacherName").isEmpty() ? reMap.get("teacherName") : null;
        int type = reMap.get("type") != null && !reMap.get("type").isEmpty() ? Integer.parseInt(reMap.get("type")) : null;
        String teacherId = reMap.get("teacherId") != null && !reMap.get("teacherId").isEmpty() ? reMap.get("teacherId") : null;
        String gradeIdList = reMap.get("gradeIdList");//批量导出

        String yearClassIdString = reMap.get("yearClassId") != null && !reMap.get("yearClassId").isEmpty() ? reMap.get("yearClassId") : null;
        String[] yearClassId = new String[0];
        if (yearClassIdString != null) {
            yearClassId = yearClassIdString.split(",");
        }

        String subjectsIdString = reMap.get("subjectsId") != null && !reMap.get("subjectsId").isEmpty() ? reMap.get("subjectsId") : null;
        String[] subjectsId = new String[0];
        if (subjectsIdString != null) {
            subjectsId = subjectsIdString.split(",");
        }

        Quarter quarterNow = iQuarterService.getNowQuarter();

        int pageNum = -1;
        int pageSize = -1;

        Page<Grade> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Grade> queryWrapper = new QueryWrapper<>();
        String status = reMap.get("status");

        if (StringUtils.isBlank(yearPart)) {
            yearPart = quarterNow.getYearPart().toString();
        }
        if (StringUtils.isBlank(quarterNum)) {
            quarterNum = quarterNow.getNum().toString();
        }

        if (StringUtils.isBlank(schoolId)) {
            queryWrapper.in("school_id", this.baseSchoolIdList);
        }

        queryWrapper.eq(StringUtils.isNotBlank(yearPart), "year_part", yearPart);
        queryWrapper.eq(StringUtils.isNotBlank(quarterNum), "quarter_num", quarterNum);
        queryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
        queryWrapper.eq(StringUtils.isNotBlank(courseId), "course_id", courseId);
        queryWrapper.eq(StringUtils.isNotBlank(classRoomId), "class_room_id", classRoomId);
        queryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);
        queryWrapper.eq(StringUtils.isNotBlank(lessonType), "lesson_type", lessonType);
        queryWrapper.inSql(StringUtils.isNotBlank(gradeIdList), "id", gradeIdList);

        if (StringUtils.isNotBlank(status)) {
            queryWrapper.eq("status", status);
        }
        if (yearClassId.length > 0) {
            queryWrapper.in("year_class_id", yearClassId);
        }
        if (subjectsId.length > 0) {
            queryWrapper.in("subjects_id", subjectsId);
        }


        queryWrapper.eq("deleted", 0);
        queryWrapper.notInSql("id", "select grade_id from change_school_content WHERE deleted = 0 AND grade_id = grade_view.id");

//1老师，2校区老师，3校长，4校区
//        if (type == 3 && teacherName != null) {
//            queryWrapper.inSql("teacher_id", "select admin_id from role_admin where role_id = 1568160887369994242 AND admin_id IN (select id from staff where id = role_admin.admin_id AND deleted = 0 AND `name` LIKE '%" + teacherName + "%')");
//        } else {
//            if (type == 3) {
//                queryWrapper.inSql("teacher_id", "select admin_id from role_admin where role_id = 1568160887369994242 ");
//            } else
        if (teacherName != null) {
            queryWrapper.inSql("teacher_id", "select id from staff where id = grade_view.teacher_id AND deleted = 0 AND `name` LIKE '%" + teacherName + "%' ");
        } else {
            if (this.sourseLeveEnum.equals(SourseLeveEnum.ONESELF)) {
                queryWrapper.eq("teacher_id", this.adminId);
            }
        }
//        }
        queryWrapper.orderByDesc("created_at");

        List<Grade> grades = iGradeService.findAll(page, queryWrapper).getRecords();

        for (Grade grade : grades) {
            if (grade.getCount() != null && grade.getFullCount() != null) {
                grade.setStudentNumber(grade.getCount() + "/" + grade.getAdvanceCount() + "/" + grade.getFullCount());
            }
            if (grade.getRecruitStatus() != null) {
                grade.setEnrollmentStatus(grade.getRecruitStatus().getValue());
            }
            if (grade.getTeacherObj() != null) {
                grade.setTeacherName(grade.getTeacherObj().getName());
            }
            if (grade.getCourseObj() != null) {
                grade.setCourseStr(grade.getCourseObj().getName());
            }
            if (grade.getStatus() != null) {
                grade.setFinishStatus(grade.getStatus().getValue());
            }
            if (grade.getSchoolObj() != null) {
                grade.setSchoolName(grade.getSchoolObj().getName());
            }
            Grade gradeById = iGradeService.getById(grade.getId());
            if (gradeById != null) {
                if (gradeById.getIsScheduling() == 1) {
                    grade.setIsSchedulingStr("已排课");
                } else {
                    grade.setIsSchedulingStr("未排课");
                }
            } else {
                grade.setIsSchedulingStr("未排课");
            }
        }

        ExcelExportHandle.export(response, "转校列表", grades, Grade.class);
        return new Result(Code.OK, grades);
    }

}
